package com.example.jpatest.controller.restful;

import com.example.jpatest.entity.Team;
import com.example.jpatest.exception.teamException.TeamExistException;
import com.example.jpatest.exception.teamException.TeamNotExistException;
import com.example.jpatest.service.impl.CompetitionServiceImpl;
import com.example.jpatest.service.impl.EnterServiceImpl;
import com.example.jpatest.service.impl.TeamServiceImpl;
import com.example.jpatest.unit.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class TeamController {

    @Autowired
    TeamServiceImpl teamService;

    @Autowired
    CompetitionServiceImpl competitionService;

    @Autowired
    EnterServiceImpl enterService;

    /**
     * 添加队伍
     */
    @PostMapping("/team")
    public Result addTeam(@RequestBody Team team){
        if (teamService.getByName(team.getName()) != null) {
            throw new TeamExistException();
        }
        teamService.saveTeam(team);
        return Result.succMsg();
    }

    /**
     * 获得队伍
     */
    @GetMapping("/team/{name}")
    public Result getTeam(@PathVariable String name){
        Team team = teamService.getByName(name);
        if(team==null){
            throw new TeamNotExistException();
        }
        return Result.succ(team);
    }

    /**
     * 获得所有队伍
     */
     @GetMapping("/teams")
     public Result getTeams(){
         return Result.succ(teamService.getTeams());
     }

    @DeleteMapping("/team/{name}")
    public Result deleteTeam(@PathVariable String name){
        Team team = teamService.getByName(name);
        if (team == null) {
            throw new TeamNotExistException();
        }
        teamService.deleteTeam(team);
        return Result.succMsg();
    }

    /**
     * 更新队伍
     */
    @PutMapping("/team")
    public Result updateTeam(@RequestBody Team team){
        Team byName = teamService.getById(team.getId());
        if (byName == null) {
            throw new TeamNotExistException();
        }
        teamService.saveTeam(team);
        return Result.succMsg();
    }

    /**
     * 获得队伍中所有学生
     */
    @GetMapping("/team/{name}/students")
    public Result getTeamStudents(@PathVariable String name){
        Team team = teamService.getByName(name);
        if (team == null) {
            throw new TeamNotExistException();
        }
        return Result.succ(teamService.getAllStudent(team));
    }

    /**
     * 获得队伍中所有老师
     */
    @GetMapping("/team/{name}/teachers")
    public Result getTeamTeachers(@PathVariable String name){
        Team team = teamService.getByName(name);
        if (team == null) {
            throw new TeamNotExistException();
        }
        return Result.succ(teamService.getTeachers(team));
    }

    /**
     * 查找队伍所有报名
     */
    @GetMapping("/team/{name}/enters")
    public Result getTeamEnters(@PathVariable String name){
        Team team = teamService.getByName(name);
        if (team == null) {
            throw new TeamNotExistException();
        }
        return Result.succ(teamService.getEnters(team));
    }
}
